<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">推广员</label>
                            <div class="layui-input-inline">
                                <select name="userId" lay-search lay-filter="userId" lay-verify="required"
                                        lay-reqtext="推广员不能为空">
                                    <option value="">请选择推广员</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/common/commonApi.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script>

    window.onload = function () {
        // 初始化推广员
        let data = {
            identity: '1'
        }
        let res = initExtensionUser(data)
        res.then((userList) => {
            layui.use(['form'], function () {
                var form = layui.form,
                    $ = layui.$;
                let options = "";
                for (const index in userList) {
                    const userMap = new Map(Object.entries(userList[index]));
                    options += "<option value='" + userMap.get("userId") + "'>" + userMap.get("realName") + "</option>";
                }
                $("select[name='userId']").append(options);// 重新渲染下拉框
                form.render('select');
            });
        })
    }


    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            table.render({
                elem: '#currentTableId',
                url: '/buying/publish/group/purchase/list',
                where: {
                    initiatorId: data.field.userId
                },
                method: 'GET',
                dataType:'json',
                contentType: 'application/x-www-form-urlencoded',
                toolbar: false,
                cols: [[
                    {
                        field: 'numbering', title: '序号', width: 60, align: 'center', templet: function (d) {
                            return d.LAY_INDEX; //返回序号
                        }
                    },
                    {
                        field: 'goodsName', title: '代理销售商品名称', sort: true, align: 'center', templet: function (d) {
                            return d.goodsName + '(' + d.goodsNo + ')'
                        }
                    },
                    {
                        field: 'distributionReward',
                        title: '代理销售佣金(元)',
                        sort: true,
                        align: 'center',
                        templet: function (d) {
                            let num = parseFloat(d.distributionReward)
                            return num.toFixed(2);
                        }
                    },
                    {field: 'usedQuantity', title: '累计销量', sort: true, align: 'center'},
                    {
                        field: 'actualIncome',
                        title: '实际收入(元)',
                        sort: true,
                        align: 'center',
                        templet: function (d) {
                            let usedQuantity = isNotNull(d.usedQuantity) ? d.usedQuantity : 0.00;
                            usedQuantity = parseInt(usedQuantity)
                            let distributionReward = isNotNull(d.distributionReward) ? d.distributionReward : 0.00;
                            distributionReward = parseFloat(distributionReward).toFixed(2);
                            let num = usedQuantity * distributionReward
                            return num.toFixed(2);
                        }
                    },
                ]],
                limits: [50, 100, 150, 200, 250, 300],
                limit: 50,
                page: true,
                request: {
                    pageName: 'pageNum',
                    limitName: 'pageSize'
                },
                response: { //定义后端 json 格式，详细参见官方文档
                    statusName: 'code', //状态字段名称
                    statusCode: '200', //状态字段成功值
                    msgName: 'msg', //消息字段
                    countName: 'total', //总数字段
                    dataName: 'rows' //数据字段
                },
            });
            return false;
        });

        // 监听表格 行双击事件
        table.on('rowDouble(currentTableFilter)', function (obj) {
            var index = layer.open({
                title: '代理销售详情',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['60%', '90%'],
                content: './groupbuy-promotion-info.html?id=' + obj.data.id,
                end: function () {
                    //关闭弹框后触发
                    // reloadTable()
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        });

    });

    /**
     * 重载表格数据
     */
    function reloadTable() {
        layui.use(['table'], function () {
            let table = layui.table;
            table.reload('currentTableId', {
                url: '/buying/publish/group/purchase/list'
            });
        })
    }

</script>

</body>
</html>